package com.teragadgets.android.comic.datamodel;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Observable;
import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class WebComic extends Observable implements Comparable<WebComic> {

	public static final Bitmap DEFAULT_BITMAP = Bitmap.createBitmap(64, 64,
			Bitmap.Config.ALPHA_8);

	public static final int NOT_SET = -1;
	public static final int DESC = 0;
	public static final int ASC = 1;

	private String id;
	private String name;
	private String base_url;
	private String logo_url;
	private int last_read;
	private Bitmap icon;
	private Vector<Comic> comics;
	private int number_of_comics;
	private int order;

	private boolean selected;

	public WebComic(String webComicTitle, String url, String webComicImage,
			String comicID, int latestComic, int lastRead, int order) {
		this.last_read = lastRead;
		this.name = webComicTitle;
		this.base_url = url;
		this.logo_url = webComicImage;
		this.id = comicID;
		this.number_of_comics = latestComic;
		this.order = order;
	}

	public WebComic(String webComicTitle, String url, String webComicImage,
			String comicID, int latestComic) {
		this.name = webComicTitle;
		this.base_url = url;
		this.logo_url = webComicImage;
		this.id = comicID;
		this.number_of_comics = latestComic;
	}

	private Bitmap loadBitmap() {
		try {
			HttpURLConnection conn = (HttpURLConnection) new URL(logo_url)
					.openConnection();
			conn.setDoInput(true);
			conn.connect();

			icon = BitmapFactory.decodeStream(conn.getInputStream());
		} catch (Exception e) {
			icon = DEFAULT_BITMAP;
		}
		return icon;
	}

	public String getName() {
		return name;
	}

	public Bitmap getIcon() {
		if (icon != null) {
			return icon;
		} else {
			loadIcon();
			return null;
		}
	}

	public String getID() {
		return id;
	}

	public String getFeed() {
		return "http://andcomicsengine.appspot.com/comicrequest.jsp?id="
				+ getID();
	}

	public String getFeed(int start, int num) {
		System.out
				.println("http://andcomicsengine.appspot.com/comicrequest.jsp?id="
						+ getID()
						+ "&start="
						+ start
						+ "&end="
						+ (start + num)
						+ "&order=" + getOrderString());
		return "http://andcomicsengine.appspot.com/comicrequest.jsp?id="
				+ getID() + "&start=" + start + "&end=" + (start + num)
				+ "&order=" + getOrderString();
	}

	private String getOrderString() {
		return order == ASC ? "asc" : "desc";
	}

	@Override
	public String toString() {
		return name;
	}

	public int getNumberOfComics() {
		return number_of_comics;
	}

	public String getURL() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getIconURL() {
		return logo_url;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public boolean getSelected() {
		return selected;
	}

	@Override
	public int compareTo(WebComic another) {
		return name.compareTo((another).getName());
	}

	public void setLastRead(int curComic) {
		this.last_read = curComic;
	}

	public int getLastRead() {
		return last_read;
	}

	public void loadIcon() {
		icon = DEFAULT_BITMAP;
		new AsyncTask<Object, Object, Bitmap>() {

			@Override
			protected Bitmap doInBackground(Object... arg0) {
				try {
					return loadBitmap();
				} catch (Exception e) {
					e.printStackTrace();
					return DEFAULT_BITMAP;
				}
			}

			@Override
			protected void onPostExecute(Bitmap bitmap) {
				setIcon(bitmap);
				setChanged();
				notifyObservers();
			}
		}.execute((Object) null);
	}

	protected void setIcon(Bitmap bitmap) {
		icon = bitmap;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public int getOrder() {
		return order;
	}

	public void changeOrder() {
		if(order == ASC)
			order = DESC;
		else
			order = ASC;
		
	}
}
